Page({
  data: {
    weeklyRecipes: [],
    loading: true
  },

  onLoad: function() {
    // 获取完整食谱数据
    this.getFullRecipe();
  },
  
  // 获取完整食谱
  getFullRecipe: function() {
    this.setData({ loading: true });
    
    wx.cloud.callFunction({
      name: 'recipeManager',
      data: {
        action: 'getRecipe'
      },
      success: res => {
        this.setData({ loading: false });
        
        if (res.result && res.result.success) {
          const recipeData = res.result.data;
          
          // 更新页面数据
          this.setData({
            weeklyRecipes: recipeData.weeklyRecipes
          });
        } else {
          wx.showToast({
            title: '获取食谱失败',
            icon: 'none'
          });
          
          // 返回上一页
          setTimeout(() => {
            wx.navigateBack();
          }, 1500);
        }
      },
      fail: err => {
        this.setData({ loading: false });
        console.error('获取食谱失败', err);
        wx.showToast({
          title: '获取食谱失败',
          icon: 'none'
        });
        
        // 返回上一页
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      }
    });
  },
  
  // 重新生成食谱
  regenerateRecipe: function() {
    wx.showModal({
      title: '提示',
      content: '确定要重新生成食谱吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '正在生成...',
            mask: true
          });
          
          wx.cloud.callFunction({
            name: 'recipeManager',
            data: {
              action: 'regenerateRecipe'
            },
            success: res => {
              wx.hideLoading();
              
              if (res.result && res.result.success) {
                wx.showToast({
                  title: '食谱已更新',
                  icon: 'success'
                });
                
                // 更新页面数据
                const recipeData = res.result.data;
                this.setData({
                  weeklyRecipes: recipeData.weeklyRecipes
                });
              } else {
                wx.showToast({
                  title: res.result.message || '生成失败',
                  icon: 'none'
                });
              }
            },
            fail: err => {
              wx.hideLoading();
              console.error('重新生成食谱失败', err);
              wx.showToast({
                title: '生成失败，请重试',
                icon: 'none'
              });
            }
          });
        }
      }
    });
  }
}); 